package org.dey.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.dey.entity.Result;
import org.dey.mapper.HealthAssessmentMapper;
import org.dey.pojo.HealthAssessment;
import org.dey.pojo.Member;
import org.dey.pojo.Order;
import org.dey.pojo.PageVo;
import org.dey.service.HealthAssessmentService;
import org.dey.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class HealthAssessmentServiceImpl extends ServiceImpl<HealthAssessmentMapper, HealthAssessment> implements HealthAssessmentService {
    @Autowired
    HealthAssessmentMapper healthAssessmentMapper;

    @Autowired
    OrderService orderService;


    @Override
    public PageVo<HealthAssessment> queryHa(HealthAssessment healthAssessment, int pageNum, int pageSize) {


//        QueryWrapper<HealthAssessment> queryWrapper = new QueryWrapper<HealthAssessment>();

        PageHelper.startPage(pageNum, pageSize);
        List<HealthAssessment> healthAssessments = healthAssessmentMapper.queryHa();
        PageInfo<HealthAssessment> pageInfo = new PageInfo<HealthAssessment>(healthAssessments);

        PageVo<HealthAssessment> pageVo = new PageVo<>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

    @Override
    public Result addHa(HealthAssessment healthAssessment) {

        int num = healthAssessment.getId();

        Result result = null;

        healthAssessment.setId(null);

        Date time = new Date();
        healthAssessment.setDateOfAssessment(time);
        boolean one = this.save(healthAssessment);

        if (one) {
            UpdateWrapper<Order> updateWrapper = new UpdateWrapper<Order>();
            updateWrapper.eq("id", num).setSql("orderStatus='已到诊'");
            boolean update = orderService.update(updateWrapper);
            if (update) {
                result = new Result(true, "添加心理测评成功");
            }

        } else {
            result = new Result(true, "添加心理测评失败");
        }
        //前端弹框取值==> res.data.message

        return result;
    }


    @Override
    public Result delHa(int id) {
        Result result = null;
        try {
            this.healthAssessmentMapper.deleteByHaid(id);
            result = new Result(true, "删除成功");
        } catch (Exception e) {
            result = new Result(true, "删除失败");
        }
        return result;
    }

    @Override
    public HealthAssessment queryHaByid(int haid) {
//        System.out.println("------------------------>"+haid);
        return this.getById(haid);
    }


    @Override
    public int updateHa(HealthAssessment healthAssessment) {
        int num = 0;
        boolean b = this.updateById(healthAssessment);
        if (b) {
            num = 1;
        }
        return num;
    }

    @Override
    public List<Member> queryTorList() {

        LocalDate now = LocalDate.now();

        ZoneId zoneId = ZoneId.systemDefault();

        Date parse = Date.from(now.atStartOfDay().atZone(zoneId).toInstant());
        return healthAssessmentMapper.queryTroid(parse);
    }



    /*@Override
    public int updateOr(int id) {
        UpdateWrapper<Order> updateWrapper =new UpdateWrapper<>();
        updateWrapper.eq("id", id).setSql("orderStatus=已就诊");
        return 0;
    }*/
}
